#include <REGX52.H>
#include "DelayTime.h"

unsigned char key = 0;

unsigned char Key_Num()
{
	// 直接返回key是不行的，按键边缘检测到了之后，会设置key，但key不会自动清0，
	// 如果按键按下一次，那么它会一直返回key，我们需要先清0，再返回回来

	// return key;
	unsigned char tmp = 0;
	tmp = key;
	return tmp;
}

int Key_State()
{
	unsigned char key_ = 0;
	if (P3_1 == 0)
	{
		key_ = 1;
	}
	if (P3_0 == 0)
	{
		key_ = 2;
	}
	if (P3_2 == 0)
	{
		key_ = 3;
	}
	if (P3_3 == 0)
	{
		key_ = 4;
	}
	return key_;
}

// 定时器执行的中断函数
void Key_Loop()
{
	static unsigned char pre = 0, cur_key = 0;
	pre = cur_key;
	cur_key = Key_State();
	// 当前状态是0,前一个状态是非0 则表明是松开按键的状态
	if (cur_key == 0 && pre == 1)
	{
		key = 1;
	}
	else if (cur_key == 0 && pre == 2)
	{
		key = 2;
	}
	else if (cur_key == 0 && pre == 3)
	{
		key = 3;
	}
	else if (cur_key == 0 && pre == 4)
	{
		key = 4;
	}
}
